//管理员账号
<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row class="operation">
            <Button @click="getDataList" icon="md-refresh">刷新</Button>
            <Button @click="add" type="primary" icon="md-add">添加</Button>
          </Row>
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
      </Col>
    </Row>
    <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="700">
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate" >
        <FormItem label="登录账号" prop="username">
          <Input v-model="form.username" autocomplete="off"/>
        </FormItem>
        <FormItem label="用戶昵称" prop="nickName">
          <Input v-model="form.nickName" autocomplete="off"/>
        </FormItem>
        <FormItem label="用户头像" prop="avatar" >
          <upload-pic-input v-model="form.avatar"></upload-pic-input>
        </FormItem>
        <FormItem label="密码" prop="password" v-if="modalType==0">
          <Input type="password" v-model="form.password" autocomplete="off"/>
        </FormItem>
        <FormItem label="邮箱" prop="email">
          <Input v-model="form.email"/>
        </FormItem>
        <FormItem label="手机号" prop="mobile">
          <Input v-model="form.mobile"/>
        </FormItem>
        <FormItem label="性别" prop="sex">
          <Select v-model="form.sex" placeholder="请选择">
            <Option value="男">男</Option>
            <Option value="女">女</Option>
          </Select>
        </FormItem>
         <FormItem label="地址" prop="address">
          <Input v-model="form.address"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>
    
    <Modal title="查看大图" v-model="visible" class-name="fl-image-modal">
      <img :src="imgUrl" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>

<script>
let sha1 = require('js-sha1');
import {
  getUserListData,
  deleteUser
} from "@/api/index";
import circleLoading from "@/views/my-components/circle-loading.vue";
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input.vue"
export default {
  name: "staffManagement",
  components: {
    circleLoading,
    uploadPicInput
  },
  data() {
    return {
      imgUrl: "",
      visible: false,
      listtype:'',
      loading: true, // 表单加载状态
      loadingEdit: false,
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalTitle: "", // 添加或编辑标题
      searchForm: { // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      userform:{
       mobile:'',
       username:'',
       pageNumber:1,
       pageSize:10,
      },
      form: { // 添加或编辑表单对象初始化数据
        id: "",
        username:'',
        password:'',
        mobile:'',
        sex:'',
        address:'',
        nickName: "",
        email: "",
        avatar: "",
        roleIds: "496138616573952"
      },
      // 表单验证规则
      formValidate: {
        username: [{ required: true, message: "登录账号不能为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        mobile: [{ required: true, message: "手机号格式不正确", trigger: "blur", pattern: /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/ }],
        sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
        address: [{ required: false, message: "地址不能为空", trigger: "blur" }],
        nickName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
        email: [{ required: false, message: "邮箱格式不正确", trigger: "blur", pattern: /^[a-z,A-Z,0-9]+@[a-z,A-Z]+.[a-z,A-Z]+$/ }],
        avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
      },
      submitLoading: false, // 添加或编辑提交状态
      columns: [
        // 表头
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "头像",
          key: "avatar",
          minWidth: 120,
          sortable: false,
          render: (h, params) => {
            return h("avatar", {
              props: {
                src: params.row.avatar
              }
            });
          }
        },
        {
          title: "登录账号",
          key: "username",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "用户昵称",
          key: "nickName",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "邮箱",
          key: "email",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "手机号码",
          key: "mobile",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "性别",
          key: "sex",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "地址",
          key: "address",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          fixed: "right",
          minWidth: 300,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                    icon: "ios-create-outline",
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.status==-1)?"none":"inline-block"
                  },
                  on: {
                    click: () => {
                      this.frozen(params.row,1);
                    }
                  }
                },
                "冻结"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.status==0)?"none":"inline-block"
                  },
                  on: {
                    click: () => {
                      this.frozen(params.row,2);
                    }
                  }
                },
                "解冻"
              ),
            ]);
          }
        }
      ],
      data: [], // 表单数据
      type:"",
      total: 0 // 表单数据总数
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    //获取用户列表
    userSearch(){
      this.postRequest("/user/getUserList",this.userform).then(res =>{
        console.log(res)
        if(res.success){
          this.data = res.result.data;
        }
        else{
          this.$Message.success("暂无数据");
        }
      })
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
      this.loading = true;
      // 带多条件搜索参数获取表单数据 请自行修改接口
      this.postRequest("/user/getBackUserAdminList", this.searchForm).then(res => {
        console.log(res)
        this.loading = false;
        if (res.success) {
          var arr = res.result.data;
          for(let val of arr){
            for(let i in val){
              (val[i] === null) ? (val[i] = "-") : ""
            }
          }
          this.data = res.result.data;
          this.total = res.result.size;
        } else if (res.code == 201) {
          this.data=[];
          this.total=0;
        }
      });
    },
    handleView(url) {
      console.log(url)
      this.imgUrl = url;
      this.visible = true;
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true;
          this.form.password = sha1(this.form.password)
          if (this.modalType === 0) {
            this.postRequest("/user/addUser", this.form).then(res => {
              this.submitLoading = false;
              console.log(res)
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.modalVisible = false;
              }
            });
          } else {
            console.log("编辑")
            this.postRequest("/user/editOwn", this.form).then(res => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.modalVisible = false;
              }
            });
          }
        }
      });
    },
    add() {
      this.modalType = 0;
      this.modalTitle = "添加";
      delete this.form.id;
      this.$refs.form.resetFields();
      this.modalVisible = true;
    },
    edit(v){
     this.modalType = 1
     this.modalTitle = "编辑";
     this.form.username = v.username;
     this.form.mobile = v.mobile;
     this.form.sex = v.sex;
     this.form.address = v.address;
     this.form.email = v.email;
     this.form.avatar = v.avatar;
     this.form.nickName = v.nickName;
     this.form.id = v.id;
     this.modalVisible = true;
    },
    frozen(v, j) {
      console.log(v)
      let params ={
        userId:v.id
      }
      this.postRequest("/user/freezeAccount",params).then(res =>{
        console.log(res)
        if(res.success){
          if (j===1) {
            this.$Message.success("冻结成功");
          } else {
            this.$Message.success("解冻成功");
          }
          this.getDataList();
        }
      })
    },
    // remove(v){
    //   this.$Modal.confirm({
    //     title: "确认删除",
    //     // 记得确认修改此处
    //     content: "您确认要删除?",
    //     loading: true,
    //     onOk: () => {
    //       deleteUser(v.id).then(res => {
    //         this.$Modal.remove();
    //         if (res.success) {
    //           this.$Message.success("删除成功");
    //           this.getDataList();
    //         }
    //       });
    //     }
    //   });
    // },
    // delAll() {
    //   console.log(this.data)
    //   if (this.selectCount <= 0) {
    //     this.$Message.warning("您还未选择要删除的数据");
    //     return;
    //   }
    //   this.$Modal.confirm({
    //     title: "确认删除",
    //     content: "您确认要删除所选的数据?",
    //     loading: true,
    //     onOk: () => {
    //       // 批量删除
    //       this.postRequest("/size/removeSize",{id:this.brandId}).then(res => {
    //         this.$Modal.remove();
    //         if (res.success) {
    //           this.$Message.success("操作成功");
    //           this.getDataList();
    //         }
    //       })
    //       // 模拟请求成功
    //       this.$Message.success("操作成功");
    //       this.$Modal.remove();
    //       this.getDataList();
    //     }
    //   })
    // }
  },
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>